package com.controller;

import com.entity.Job;
import com.util.DataSourceUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;

@WebServlet("/getjob1")
public class GetJob1Servlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
    {
        Job job=new Job();
        String sql="select * from Job2 where jid=?";
        try (Connection con = DataSourceUtils.getConnection();
             PreparedStatement ps=con.prepareStatement(sql)){
            ps.setString(1,req.getParameter("jid"));
            try(ResultSet rs=ps.executeQuery())
            {
                rs.next();
                job.setJid(rs.getString("jid"));
                job.setTitle(rs.getString("Title"));
                job.setDateTime(rs.getObject("PublishDate", LocalDateTime.class));
                job.setContent(rs.getString("Content"));
                job.setImageurl(rs.getString("ImageURL"));
            }
        }
        catch (SQLException e)
        {
            throw new RuntimeException(e);
        }
        req.setAttribute("job",job);
        req.getRequestDispatcher("/WEB-INF/jsp/jobb1.jsp").forward(req,resp);
    }
}
